/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.alarm.overview;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


import twaver.Alarm;
import twaver.TWaverUtil;
import twaver.swing.LabelValueLayout;

public class AlarmUITimePane extends JPanel {
    private class AcknowledgeTextField extends JTextField {
        private boolean acked = false;

        public void setAcked(boolean acked) {
            this.acked = acked;
            this.repaint();
        }

        public void setText(String text){
            super.setText("");
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (acked) {
                Icon icon = TWaverUtil.getIcon("/resource/image/swing/check.png");
                int y = (this.getHeight() - icon.getIconHeight()) / 2;
                int x = 16;
                icon.paintIcon(null, g, x, y);
            }
        }
    }
    private class ClearedTextField extends JTextField {
            private boolean cleared = false;
            private Icon clearedIcon=TWaverUtil.getIcon("/demo/resource/images/cleared.gif");

            public void setCleared(boolean cleared) {
                this.cleared = cleared;
                if(cleared){
                    this.setBackground(Color.green);
                }else{
                    this.setBackground(null);
                }
                this.repaint();
            }

            public void setText(String text){
                super.setText("");
            }

            public void paintComponent(Graphics g) {
                super.paintComponent(g);

                if (cleared) {
                    int y = (this.getHeight() - clearedIcon.getIconHeight()) / 2;
                    int x = 16;//(this.getWidth() - icon.getIconWidth()) / 2;
                    clearedIcon.paintIcon(null, g, x, y);
                }
            }
        }

    LabelValueLayout layout = new LabelValueLayout(10, 2);
    JLabel lbAcked = new JLabel();
    JTextField txtLastChangeTime = new JTextField();
    JLabel lbAckTime = new JLabel();
    AcknowledgeTextField txtAcked = new AcknowledgeTextField();
    JLabel lbAckUserID = new JLabel();
    JTextField txtAckUserID = new JTextField();
    JLabel lbRaisedTime = new JLabel();
    JTextField txtRaisedTime = new JTextField();
    JLabel lbLastChangeTime = new JLabel();
    JTextField txtAckTime = new JTextField();
    JLabel lbCleared = new JLabel();
    ClearedTextField txtCleared = new ClearedTextField();
    JLabel lbClearTime = new JLabel();
    JTextField txtClearTime = new JTextField();
    JLabel lbClearUserID = new JLabel();
    JTextField txtClearUserID = new JTextField();

    public AlarmUITimePane() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    void jbInit() throws Exception {
        lbAcked.setText("Acknowledged:");
        this.setLayout(layout);
        lbAckTime.setText("Acknowledged time:");
        lbAckUserID.setText("Acknowledge user ID:");
        lbRaisedTime.setText("Raised time:");
        lbLastChangeTime.setText("Last change time:");
        txtAcked.setEditable(false);
        txtAcked.setText("");
        txtLastChangeTime.setEditable(false);
        txtLastChangeTime.setText("");
        txtAckUserID.setEditable(false);
        txtAckUserID.setText("");
        txtRaisedTime.setEditable(false);
        txtRaisedTime.setText("");
        txtAckTime.setEditable(false);
        txtAckTime.setText("");
        this.txtCleared.setText("");
        this.txtCleared.setEditable(false);
        lbClearTime.setToolTipText("");
        lbClearTime.setText("Cleared time:");
        this.lbCleared.setText("Cleared:");
        txtClearTime.setEditable(false);
        lbClearUserID.setText("Clear user ID:");
        txtClearUserID.setEditable(false);
        this.add(lbLastChangeTime, null);
        this.add(txtLastChangeTime, null);
        this.add(lbRaisedTime, null);
        this.add(txtRaisedTime, null);
        this.add(lbAcked, null);
        this.add(txtAcked, null);
        this.add(lbAckTime, null);
        this.add(txtAckTime, null);
        this.add(lbAckUserID, null);
        this.add(txtAckUserID, null);
        this.add(lbCleared, null);
        this.add(txtCleared, null);
        this.add(lbClearTime, null);
        this.add(txtClearTime, null);
        this.add(lbClearUserID, null);
        this.add(txtClearUserID, null);
    }

    public void setAlarm(Alarm alarm) {
        reset();
        this.txtAcked.setAcked(alarm.isAcked());
        this.txtAckTime.setText(TWaverUtil.getTimeLocalString(alarm.getAckTime()));
        this.txtAckUserID.setText(alarm.getAckUserID());
        this.txtCleared.setCleared(alarm.isCleared());
        this.txtClearTime.setText(TWaverUtil.getTimeLocalString(alarm.getClearedTime()));
        this.txtClearUserID.setText(alarm.getClearUserID());
        this.txtLastChangeTime.setText(TWaverUtil.getTimeLocalString(alarm.getLastChangedTime()));
        this.txtRaisedTime.setText(TWaverUtil.getTimeLocalString(alarm.getRaisedTime()));
    }

    private void reset() {
        this.txtAcked.setAcked(false);
        this.txtCleared.setCleared(false);
        this.txtAckTime.setText("");
        this.txtAckUserID.setText("");
        this.txtClearTime.setText("");
        this.txtClearUserID.setText("");
        this.txtLastChangeTime.setText("");
        this.txtRaisedTime.setText("");
    }
}